<?php

class IncidentController extends Zend_Controller_Action
{

	public function init()
	{
		$this->view->addHelperPath(
		'ZendX/JQuery/View/Helper', 
		'ZendX_JQuery_View_Helper');
	}

	public function indexAction()
	{
		// action body
	}

	public function addAction()
	{
		$form = new Application_Form_Incident();
		$envoyer = new Zend_Form_Element_Submit('envoyer');
		$envoyer->setAttrib('id', 'boutonenvoyer');
		$form->addElements(array($envoyer));
		$form->envoyer->setLabel('Declarer');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
					
				$materiel=$form->getValue('idMateriel');
				$datePanne=$form->getValue('datePanne');
				$employe=$form->getValue('idEmploye');
				$typePanne=$form->getValue('typePanne');
				$resumePanne = $form->getValue('resumePanne');
				$details=$form->getValue('details');
				$situation=3;

				$incidents = new Application_Model_DbTable_Incidents();
				$incidents->addIncident($materiel,$datePanne,$employe,$typePanne,$resumePanne,$details,$situation);
				//				$config = array('auth' => 'login',
				//                'username' => 'dunord_admin@swaggy.org',
				//                'password' => 'dunord',
				//				'ssl' => 'tls',
				//            	'port' => 465
				//				);
				//				$transport = new Zend_Mail_Transport_Smtp('ssl0.ovh.net',$config);
				//
				//				$mail = new Zend_Mail();
				//				$mail->setBodyText('This is the text of the mail.'); //Alternatively using setBodyHtml for HTML messages
				//				$mail->setFrom('dunord_admin@swaggy.org', 'Some Sender'); //Adds a 'from' header
				//				$mail->addTo('dugoboss@hotmail.com', 'greg');
				//				$mail->setSubject('TestSubject');
				//				$mail->send($transport);
				$this->_helper->redirector('listcurrent');
			} else {
				$form->populate($formData);
			}
		}
	}

	public function updateAction()
	{
		$form = new Application_Form_IncidentUpdating();
		$envoyer = new Zend_Form_Element_Submit('envoyer');
		$envoyer->setAttrib('id', 'boutonenvoyer');
		$form->addElements(array($envoyer));
		$form->envoyer->setLabel('Mettre a jour');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$id= $form->getValue('id');
				$materiel=$form->getValue('idMateriel');
				$datePanne=$form->getValue('datePanne');
				$employe=$form->getValue('idEmploye');
				$typePanne=$form->getValue('typePanne');
				$resumePanne = $form->getValue('resumePanne');
				$details=$form->getValue('details');
				$situation=$form->getValue('idSituation');
				$dateResolution=$form->getValue('dateResolution');
				$cout=$form->getValue('cout');
				$solution=$form->getValue('solution');

				$incidents= new Application_Model_DbTable_Incidents();
				$incidents->updateIncident($id,$materiel, $datePanne, $employe, $typePanne,$resumePanne, $details, $situation,$dateResolution, $cout,$solution);
				if ($situation==2){
					$materiels = new Application_Model_DbTable_Materiels();
					$materiels -> setBrokenMateriel($materiel);
				}
				$this->_helper->redirector('listcurrent');

			} else {
				$form->populate($formData);
			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$incidents = new Application_Model_DbTable_Incidents();
				$form->populate($incidents->getIncident($id));
			}
		}


	}

	public function listcurrentAction()
	{
		$incidents = new Application_Model_DbTable_Incidents();
		$materiel = new Application_Model_DbTable_Materiels();
		$listIncidents= $incidents->listCurrentIncident();
		Zend_Loader::loadfile('SituationSelect.php',APPLICATION_PATH.'/forms/element/',false);
		$selectSituations = new Application_Form_Element_SituationSelect('situations');


		$this->view->listIncidents = $incidents->listCurrentIncident();
		$this->view->selectSituation = $selectSituations;



	}

	public function listallAction()
	{
		$incidents = new Application_Model_DbTable_Incidents();
		$materiel = new Application_Model_DbTable_Materiels();
		$listIncidents= $incidents->listAllIncident();
		$nb=sizeof($listIncidents);

		$this->view->listIncidents = $listIncidents;



	}

	public function changesituationAction()
	{
	$situation = $_POST['idSituation'];
	$incident= $_POST['idIncident'];
	if (!empty($_POST['dateResolution']))
		$dateResolution = $_POST['dateResolution'];
	else 
		$dateResolution = null;
		
	if (!empty($_POST['cout']))
		$cout = $_POST['cout'];
	else 
		$cout = null;
	if (!empty($_POST['solution']))
		$solution = $_POST['solution'];
	else 
		$solution = null;
	
	
		$incidents= new Application_Model_DbTable_Incidents();
		$incidents->changeSituationIncident($incident, $situation,$dateResolution, $cout,$solution);
		
		if ($situation==2){
			$incident = $incidents->getIncident($incident);
			$materiel = $incident['idMateriel'];
			$materiels = new Application_Model_DbTable_Materiels();
			$materiels -> setBrokenMateriel($materiel);
		}
		


}

}



